package edu.gatech.cs2340.group7.wheresmystuffapp.junit;

import edu.gatech.cs2340.group7.wheresmystuffapp.model.Account;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.User;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.LoginView;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Login Activity tester
 * @author Karim Amirali
 * @version 1.0
 */

public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginView> {

	LoginView activity;
	EditText etAskEmail;
	EditText etAskPassword;
	Button bLogin;
	Button bSignUp;
	TextView tvFailedEntry;
	Account testUser;

	public void testLogin(){
		activity.runOnUiThread(
				new Runnable() {
					public void run() {
						try{
							etAskEmail.setText("Invalid Username");
							etAskPassword.setText("Invalid Password");
							bLogin.callOnClick();
							assertEquals("Invalid username and password", null, Account.getCurrentUser());
					
							etAskEmail.setText("kamirali");
							etAskPassword.setText("Invalid Password");
							bLogin.callOnClick();
							assertEquals("Invalid password for valid username", null, Account.getCurrentUser());
					
							etAskEmail.setText("Invalid Username");
							etAskPassword.setText("password");
							bLogin.callOnClick();
							assertEquals("Invalid username for valid password", null, Account.getCurrentUser());
					
							etAskEmail.setText("karim");
							etAskPassword.setText("Password");
							bLogin.callOnClick();
							assertEquals("Did not accept valid username and password", testUser, Account.getCurrentUser());
						}
						catch(NullPointerException e){
							e.printStackTrace();
						}
					}
				});

	}

	public LoginActivityTest() {
		super(LoginView.class);
		// TODO Auto-generated constructor stub
	}
	
	protected void setUp() throws Exception {
		super.setUp();
		
		setActivityInitialTouchMode(false);
		
		activity = getActivity();
		
		etAskEmail = (EditText)activity.findViewById(edu.gatech.cs2340.group7.wheresmystuffapp.R.id.et_ask_email);
		etAskPassword = (EditText)activity.findViewById(edu.gatech.cs2340.group7.wheresmystuffapp.R.id.et_ask_password);
		bLogin = (Button)activity.findViewById(edu.gatech.cs2340.group7.wheresmystuffapp.R.id.login_button);
		bSignUp = (Button)activity.findViewById(edu.gatech.cs2340.group7.wheresmystuffapp.R.id.signup_button);
		tvFailedEntry = (TextView)activity.findViewById(edu.gatech.cs2340.group7.wheresmystuffapp.R.id.failed_entry);
		
		testUser = new Account("kamirali", "password", "2340", false);
		
		User.setCurrentUser(null);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

}
